package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class checkintime
 */
@WebServlet("/checkintime")
public class checkintime extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public checkintime() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		int count_stu;
		String total = null;
		String absence=null;
		String count = request.getParameter("count");// 获取班级人数
		count_stu = Integer.parseInt(count);
		String classtime = request.getParameter("classtime");// 获取当日课时
		String classname = request.getParameter("classname");
		String centername = request.getParameter("centername");
		String checktable = centername + classname + "check";// 获得将要写入的考勤表格名称
		String check_date = request.getParameter("checkdate");// 获取考勤日期(未转变格式的日期)
		String insert = "insert into checkdate(centername,classname,date,classtime) values('"+centername+"','"+classname+"','" + check_date + "','"+classtime+"')";
		try {
			Jdbc checkdate1 = new Jdbc(insert);
		} catch (ClassNotFoundException | SQLException e1) {
			// TODO Auto-generated catch block
			
		}

		// =======================================
		String[] b = check_date.split("-");// 并且将2018-01-13转换为2018年01月13号
		System.out.println(Arrays.toString(b));
		String c = b[0] + "年" + b[1] + "月" + b[2] + "日";
		// C为要插入的属性名称，下面还要用
		// 建立相对应的签到列，一次插进入两行
		String alter = "alter table " + checktable + " add column " + c + " varchar(10) character set utf8 ";
		try {
			Jdbc j1 = new Jdbc(alter);

		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 第一遍循环将表的列添加了
		for (int i = 0; i < count_stu; i++) {
			// 获取从前台一起传过来的对象，并且遍历获取
			String stuid = request.getParameter("stuid" + i);
			String stuname = request.getParameter("stuname" + i);
			String check = request.getParameter("check" + i);
			// 此处，获得check_info表中的count值确定插入的checki的位置
//			String sc1 = "select count from check_info where stuid='" + stuid + "'";
//			Jdbc jsc = new Jdbc();
//			try {
//				jsc.Jdbc_Q2(sc1);
//				//结果集还要.first()将结果集的光标移到第一行结果去
//				jsc.rs.first();
//				checkcount = jsc.rs.getString("count");
//				System.out.println(checkcount);
//			} catch (ClassNotFoundException | SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
			// 首先得判断是否出席根据CHECK的值进行判断
			if (check.equals("1")) {
				// 先将记录填写到checkinfo表中去
//				String checkinfo=check_date+"出勤";
//				String update = "update check_info set check" + checkcount + "='" +checkinfo+ "' where stuid='"
//						+ stuid + "'";
//				System.out.println(update);
//				try {
//					Jdbc checki = new Jdbc(update);
//				} catch (ClassNotFoundException | SQLException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				//将checktable中的absence中的值加一
				String updateabsence = "update "+checktable+" set absence=absence+1 where stuid='"+stuid+"'";
				try {
					Jdbc ua = new Jdbc(updateabsence);
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (classtime.equals("1")) {
					String cutclass = "update contract set restclass = restclass-1 where id='" + stuid + "' ";
					try {
						Jdbc cut1 = new Jdbc(cutclass);
					} catch (ClassNotFoundException | SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				if (classtime.equals("1.5")) {
					String cutclass1 = "update contract set restclass = restclass-1.5 where id='" + stuid + "' ";
					try {
						Jdbc cut2 = new Jdbc(cutclass1);
					} catch (ClassNotFoundException | SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} // 只有满足出勤的人时候才会将SALES表中的值减去
			if(check.equals("0")) {
				//将check_info的checki的值改成 日期+未出勤 同时还要更新check_info表中的absence,将此数字加一
//				String checkinfo=check_date+"未出勤";
//				String update = "update check_info set check" + checkcount + "='" +checkinfo+ "' where stuid='"
//						+ stuid + "'";
//				String update1 = "update check_info set absence = absence+1 where stuid='"+stuid+"'";
//				try {
//					Jdbc checki = new Jdbc(update);
//					Jdbc absence = new Jdbc(update1);
//				} catch (ClassNotFoundException | SQLException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
			}
			
			
			
				// 将check_info表中的count的值再次更新，以便下次检查时候再使用
			String update1 = "update check_info set count=count+1 where stuid='" + stuid + "'";
			try {
				Jdbc updatecount = new Jdbc(update1);
			} catch (ClassNotFoundException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// 将考勤登记到表中去
			String update = "update " + checktable + " set " + c + "='" + check + "' where stuid='" + stuid + "'";
			String updatetotal="update "+ checktable+" set total=total+1 where stuid='"+stuid+"'";
			try {
				Jdbc j2 = new Jdbc(update);
				j2 = new Jdbc(updatetotal);
			} catch (ClassNotFoundException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

		}
		request.getRequestDispatcher("showsuccess.jsp").forward(request, response);
	}

}
